package net.huashitong.oversee.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Project.entity.NewProjects;
import net.huashitong.Project.service.NewProjectsService;
import net.huashitong.oversee.dao.NodeScoreDao;
import net.huashitong.oversee.entity.GradePoint;
import net.huashitong.oversee.entity.NodeScore;
import net.huashitong.oversee.entity.ScoreSummary;
import net.huashitong.user.entity.BnpUser;
import net.huashitong.user.filter.BnpUserFilter;
import net.huashitong.user.service.BnpUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static net.huashitong.utils.UtilsTools.getCurrentDateTime1;

@Service
public class NodeScoreService extends StringPKService<NodeScore> {

    @Autowired
    private NodeScoreDao dao;
    @Autowired
    private GradePointService gradePointService;
    @Autowired
    private NewProjectsService newProjectsService;
    @Autowired
    private BnpUserService bnpUserService;


    @Override
    protected BaseDao<NodeScore, String> getDao() {
        return dao;
    }


    public List<ScoreSummary> updateLeaderScoreSummary() {
        //查询所有项目
        List<NewProjects> allProjects = newProjectsService.query();
        //将分管领导的项目归纳成一个list
        Map<String, List<NewProjects>> leaderProjectMap = new HashMap<>();
        for (NewProjects project : allProjects) {
            if (!leaderProjectMap.containsKey(project.getResUser() + "-" + project.getResPhone())) {
                List<NewProjects> list = new ArrayList<>();
                list.add(project);
                leaderProjectMap.put(project.getResUser() + "-" + project.getResPhone(), list);
            } else {
                List<NewProjects> list = leaderProjectMap.get(project.getResUser() + "-" + project.getResPhone());
                list.add(project);
                leaderProjectMap.put(project.getResUser() + "-" + project.getResPhone(), list);
            }
        }
        //查询所有领导
        BnpUserFilter userFilter = new BnpUserFilter();
        userFilter.setBranchType("6");
        List<BnpUser> leaders = bnpUserService.queryByFilter(userFilter);

        Map<String, BnpUser> leadersMap = new HashMap<>();
        for (BnpUser leader : leaders) {
            if (!leadersMap.containsKey(leader.getSysId())) {
                leadersMap.put(leader.getSysId(), leader);
            }
        }
        //绩效得分（领导Id，总的绩效得分）
        Map<String, Double> userScoreMap = new HashMap<>();
        //绩效考核次数（领导Id，总的绩效个数）
        Map<String, Integer> userCountMap = new HashMap<>();
        for (BnpUser leader : leaders) {
            String leaderKey = leader.getName() + "-" + leader.getPhone();
            for (Map.Entry<String, List<NewProjects>> entry : leaderProjectMap.entrySet()) {
                String key = entry.getKey();
                if (key.equals(leaderKey)) {
                    String leaderSysId = leader.getSysId();
                    List<GradePoint> gradePoints = new ArrayList<>();
                    //获取该领导的相关所有绩效得分
                    List<NewProjects> projects = entry.getValue();
                    for (NewProjects project : projects) {
                        List<GradePoint> gp = gradePointService.queryByProjectId(project.getSysId());
                        gradePoints.addAll(gp);
                    }
                    Double allScore = 0.0;
                    for (GradePoint g : gradePoints) {
                        Double score = Double.parseDouble(StringUtils.isBlank(g.getSoucre()) ? "0.0" : g.getSoucre());
                        allScore = allScore + score;
                    }
                    if (allScore != 0.0) {
                        userScoreMap.put(leaderSysId, allScore);
                        userCountMap.put(leaderSysId, gradePoints.size());
                    } else {
                        userScoreMap.put(leaderSysId, 0.0);
                        userCountMap.put(leaderSysId, 0);
                    }
                }
            }
        }
        List<ScoreSummary> scoreSummariesResult = new ArrayList<>();
        for (Map.Entry<String, Double> entry : userScoreMap.entrySet()) {
            ScoreSummary summary = new ScoreSummary();
            String userId = entry.getKey();
            Double allScore = entry.getValue();
            Double resScore = 0.0;
            if (ObjectUtils.isNotBlank(leadersMap.get(userId))) {
                if (userCountMap.get(userId) != 0) {
                    //领导项目得分计算公式 -zzc
                    resScore = allScore / (userCountMap.get(userId)) +
                            allScore / 10;
                } else {
                    resScore = 0.0;
                }
                summary.setCheckDept(leadersMap.get(userId).getBranch());
                summary.setCheckObject(leadersMap.get(userId).getName());
                summary.setScore(String.format("%.2f", resScore) + "");
                summary.setUserId(leadersMap.get(userId).getSysId());
                summary.setSummaryDate(getCurrentDateTime1());
                summary.setBranchType(leadersMap.get(userId).getBranchType());
            }
            scoreSummariesResult.add(summary);
        }
//        Map<String, Object> resMap = new HashMap<>();
//        resMap.put("result", scoreSummariesResult);
        return scoreSummariesResult;
    }

}
